package day8;

import banking.domain.OverdraftException;

public class CheckingAccount extends Account {
    // 透支保护属性
    private double overdraftProtection;

    // 带有一个参数的构造器
    public CheckingAccount(double balance) {
        super(balance);
        this.overdraftProtection = 0.0;
    }

    // 带有两个参数的构造器
    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.overdraftProtection = protect;
    }

    // 获取透支保护额度方法
    public double getOverdraftProtection() {
        return overdraftProtection;
    }

    // 重写 withdraw 方法使它不返回值（即 void）.声明方法抛出 overdraftException 异常
    @Override
    public void withdraw(double amount) throws OverdraftException {
        if (amount <= 0) {
            throw new IllegalArgumentException("Withdrawal amount must be positive.");
        }

        if (balance >= amount) {
            balance -= amount;
        } else {
            double overdraftAmount = amount - balance;
            if (overdraftProtection >= overdraftAmount) {
                balance = 0.0;
                overdraftProtection -= overdraftAmount;
            } else {
                if (overdraftProtection == 0) {
                    throw new OverdraftException("No overdraft protection", overdraftAmount);
                } else {
                    throw new OverdraftException("Insufficient funds for overdraft protection", overdraftAmount);
                }
            }
        }
    }
}